using System.Text.RegularExpressions;
using Xunit;
using YTS.Tools;

namespace LanguageLearnTestCase.String
{
    public class RegexToInt32Value
    {
        [Theory]
        [InlineData(null, 0, -1)]
        [InlineData("", 0, -1)]
        [InlineData("33", 0, -1)]
        [InlineData("234", 0, -1)]
        [InlineData("2345", 0, -1)]
        [InlineData("5_33", 5, 33)]
        [InlineData("145_33", 145, 33)]
        [InlineData("-1_32", -1, 32)]
        [InlineData("5_-1", 5, -1)]
        [InlineData("5_-122", 5, -122)]
        [InlineData("-22_-122", -22, -122)]
        [InlineData("fef_-122", 0, -122)]
        [InlineData("55_jjj", 55, -1)]
        [InlineData("fef_jjj", 0, -1)]
        public void Decimal_ToStringF2(string inputStr, int resultParentId, int resultValue)
        {
            ToParentIdAndValue(inputStr, out int ParentId, out int Value);
            Assert.Equal(resultParentId, ParentId);
            Assert.Equal(resultValue, Value);
        }

        private void ToParentIdAndValue(string input, out int ParentId, out int Value)
        {
            ParentId = 0;
            Value = -1;
            input = input ?? string.Empty;
            var re = new Regex("([^\\s]+)_([^\\s]+)");
            if (!re.IsMatch(input))
                return;
            Match match = re.Match(input);
            ParentId = ConvertTool.ToInt(match.Groups[1].Value, ParentId);
            Value = ConvertTool.ToInt(match.Groups[2].Value, Value);
        }
    }
}
